CmdUtils.CreateCommand({
  name: "textilize-doi", 
  homepage: "http://code.google.com/p/tl-ubiquity-commands/",
  author: { name: "Martin Fenner", email: "martin.fenner@mh-hannover.de"}, 
  description: "Formats DOI as link in textile markup",
  license: "GPL",
  takes: {"doi": noun_arb_text},

  preview: function( pblock, doi ) {
    var msg = 'Inserts a textile link for the DOI: <i>${doi}</i>';
    var d = doi.text || CmdUtils.getSelection();
    pblock.innerHTML = CmdUtils.renderTemplate(msg, {doi: d});
  },

  execute: function( doi ) {
    var msg = '"doi:${doi}":http://dx.doi.org/${doi}';
    var d = doi.text || CmdUtils.getSelection();
    var newText = CmdUtils.renderTemplate(msg, {doi: d});

    var doc = context.focusedWindow.document;
    if (doc.designMode == "on")
      doc.execCommand("insertHTML", false, newText);
    else
      var focused = context.focusedElement;
      var selectionEnd = focused.selectionStart + newText.length;
      var currentValue = focused.value;
      var beforeText = currentValue.substring(0, focused.selectionStart);
      var afterText = currentValue.substring(focused.selectionEnd, currentValue.length);
      focused.value = beforeText + newText + afterText;
      focused.focus();
      //put the cursor after the inserted text
      focused.setSelectionRange(selectionEnd, selectionEnd);
  }
})